﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
namespace Common
{
  public class FileHelper
    {
      public static Hashtable FileToHashTable(String FilesPath)
      {
          Hashtable ht=new Hashtable();
          if (File.Exists(FilesPath))
          {
              StreamReader sr = new StreamReader(FilesPath,System.Text.Encoding.UTF8);
              string strRead = sr.ReadToEnd();
              string[] strs = strRead.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
              foreach(string s in strs)
              {
                  if (!string.IsNullOrEmpty(s))
                  {
                      if (ht.ContainsKey(s) && ht[s] != null)
                      {
                          int Count = (Int32)ht[s] + 1;

                          ht[s] = Count;
                      }
                      else
                      {
                          ht.Add(s, 1);
                      }
                  }
              }
              //if (File.Exists(FilesPath))
              //{
              //    File.Delete(FilesPath);
              //}
              return ht;
          }
          else
          {
              return ht;
          }
      }

      protected void StringToFile(string msg,string FileName,string Path)
      {
          if (!Directory.Exists(Path))//如果不存在就创建file文件夹
          {
              Directory.CreateDirectory(Path);
          }
          if (!File.Exists(Path+@"\"+FileName))
          {
              StreamWriter sw = new StreamWriter(Path + @"\" + FileName, true, Encoding.UTF8);
              sw.WriteLine(msg);
              sw.Close();
          }
      }
    }
}
